import os
import shutil


def restore_structure(folder_a, folder_b, folder_c):
    # 获取文件夹A的所有文件相对路径
    for root, _, files in os.walk(folder_a):
        for file in files:
            relative_path = os.path.relpath(os.path.join(root, file), folder_a)
            source_file = os.path.join(folder_b, os.path.basename(file))
            target_path = os.path.join(folder_c, relative_path)

            if os.path.exists(source_file):
                # 创建目标文件夹
                os.makedirs(os.path.dirname(target_path), exist_ok=True)
                # 复制文件到目标位置
                shutil.copy2(source_file, target_path)

# 文件夹A路径、文件夹B路径和还原路径C
folder_a = "/home/champrin/Desktop/MV-CS016-10UC+DA1041860/给预备役/json_annotations (copy)/good (copy)"
folder_b = "/home/champrin/Desktop/MV-CS016-10UC+DA1041860/给预备役/json_annotations (copy)/finish check all (copy)"
folder_c = "/home/champrin/Desktop/MV-CS016-10UC+DA1041860/给预备役/json_annotations (copy)/good restore"
os.makedirs(folder_c, exist_ok=True)

# 还原文件夹结构
restore_structure(folder_a, folder_b, folder_c)
print(f"文件已按照文件夹A的结构还原到 {folder_c}")